package com.qf.service;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 15:30
 */

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品类目
 */
@Service
public class DtsCategoryServiceimpl implements DtsCategoryService {
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Override
    public IPage<DtsCategory> FindAll  (Integer page,
                                       Integer limit,
                                       Integer id, String name ,
                                       String sort ,
                                       String order) {

        IPage<DtsCategory> page1 = new Page<>(page,limit);
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        if(id!=null){
            queryWrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("desc")) {
                queryWrapper.orderByDesc(sort);
            }else{
                queryWrapper.orderByAsc(sort);
            }
        }
        IPage<DtsCategory> dtsCategoryIPage = dtsCategoryMapper.selectPage(page1, queryWrapper);

        return dtsCategoryIPage;
    }
    //管理员名字
    @Override
    public List<CatVo>  findNameAndID(){
        List<CatVo> idAndName = dtsCategoryMapper.findIdAndName();
        return idAndName;
    }
    //查询一级类目
    @Override
    public List<CatVo> finCatvo(){
        List<CatVo> findlevel = dtsCategoryMapper.findlevel();
        return findlevel;
    }
    //查询二级类目
    @Override
    public List<DtsCategory> findleveL2() {
        List<DtsCategory> catVos = dtsCategoryMapper.findlevelL2();
        return catVos;
    }

    @Override
    public void add(DtsCategory dtsCategory) {
        dtsCategory.setDeleted(false);
        dtsCategory.setAddTime(new Date());
        dtsCategory.setUpdateTime(new Date());
        int insert = dtsCategoryMapper.insert(dtsCategory);
    }

    @Override
    public void update(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryMapper.updateById(dtsCategory);
    }

    @Override
    public void delete(DtsCategory dtsCategory) {
        dtsCategory.setDeleted(true);
        dtsCategoryMapper.updateById(dtsCategory);

    }


}
